package com.turing.pre.processor.schedule;


import com.turing.common.config.ServerConfig;
import com.turing.common.response.ResponseVO;
import com.turing.pre.processor.domain.PreProcessorTask;
import com.turing.pre.processor.enums.PreProcessorStatusEnum;
import com.turing.pre.processor.inner.FileTaskInnerService;
import com.turing.pre.processor.service.PreProcessorTaskService;
import com.turing.pre.processor.vo.in.FileTaskVo;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.util.*;

@Component
public class PreProcessorScheduleService {

    @Value( "${fdfs.domain}" )
    private String FDFS_FILE_DOMAIN;

    @Autowired
    private ServerConfig serverConfig;

    @Autowired
    private PreProcessorTaskService preProcessorTaskService;

    @Autowired
    private FileTaskInnerService fileTaskInnerService;

    private static final Log logger = LogFactory.getLog(PreProcessorScheduleService.class);

    private String sep = File.separator;

    // pull task
    @Scheduled(fixedDelay = 1000)
    @Transactional(propagation = Propagation.REQUIRED)
    public void pullPreProcessTask(){
        int count = preProcessorTaskService.countPendingTasks();
        // server free
        if(count == 0){
            String url = serverConfig.getUrl();
            try{
                // do fetch
                ResponseVO<FileTaskVo> getResVo = preProcessorTaskService.getAndLockPreProcessTask(url);
                // TODO: 2022/11/17 存在网络的问题，可能导致数据不能保存到processor中
                if(getResVo.getCode().equals("200")){
                    FileTaskVo fileTaskVo = getResVo.getData();
                    if(!fileTaskVo.equals(new FileTaskVo())){
                        // save task info to db
                        preProcessorTaskService.savePreTask(fileTaskVo);
                    }
                }
            }catch ( Exception e ){
                logger.error(e);
            }
        }
    }

    // native schedule
    @Scheduled(fixedDelay = 1000)
    @Transactional(propagation = Propagation.REQUIRED)
    public void word2pdf() {

        String url = serverConfig.getUrl();
        List<String> fileTypes = Arrays.asList("doc", "docx");
        //等待文件到pdf查询这个状态的数据
        PreProcessorTask preProcessorTask = preProcessorTaskService.getTaskByMainStatus(PreProcessorStatusEnum.WAITINGFILE2PDF.getCode(), url, fileTypes);
        Optional.ofNullable(preProcessorTask).ifPresent(p->preProcessorTaskService.doWord2pdf(p));

    }

    // native schedule
    @Scheduled(fixedDelay = 1000)
    @Transactional(propagation = Propagation.REQUIRED)
    public void txt2pdf() {

        String url = serverConfig.getUrl();
        List<String> fileTypes = Collections.singletonList("txt");
        PreProcessorTask preProcessorTask = preProcessorTaskService.getTaskByMainStatus(PreProcessorStatusEnum.WAITINGFILE2PDF.getCode(), url, fileTypes);
        Optional.ofNullable(preProcessorTask).ifPresent(p->preProcessorTaskService.doTxt2pdf(p));

    }

    @Scheduled(fixedDelay = 1000)
    public void pdf2jpg() {

        String domain = serverConfig.getUrl();
        List<String> fileTypes = Arrays.asList("doc", "docx", "pdf","txt");
        PreProcessorTask preProcessorTask = preProcessorTaskService.getTaskByMainStatus(PreProcessorStatusEnum.WAITINGPDF2JPG.getCode(), domain, fileTypes);
        Optional.ofNullable(preProcessorTask).ifPresent(p->preProcessorTaskService.doPdf2jpg(p));

    }

    @Scheduled(fixedDelay = 1000)
    public void photo2jpg() {

        String domain = serverConfig.getUrl();
        List<String> normalTypes = Arrays.asList("tif","tiff","jpeg","png","bmp");
        PreProcessorTask preProcessorTask = preProcessorTaskService.getTaskByMainStatus(PreProcessorStatusEnum.WAITINGPHOTO2JPG.getCode(), domain, normalTypes);
        Optional.ofNullable(preProcessorTask).ifPresent(p->preProcessorTaskService.doPhoto2jpg(p));

    }

}
